import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class ARPFace extends JFrame{
	
	private static final long serialVersionUID = 12L;
	
	private	JLabel 				lab 			= new JLabel("输入局域网号：");
	private	JTextField 			text 			= new JTextField(15);
	static  JTextArea 			textArea 		= new JTextArea();
	private JPanel    			topBar			= new JPanel();
	private JButton				start 			= new JButton("开始");
	private JButton				stop 			= new JButton("停止");
	private JButton				clear 			= new JButton("重置");
	private JButton				close			= new JButton("关闭");
	private Font 				buttonFont 		= new Font("宋体",Font.BOLD,30);
	Toolkit 					kit 			= Toolkit.getDefaultToolkit();
	Dimension 					screenSize		= kit.getScreenSize();
	static 	boolean 			flag			= true;					//重要判断标志——用于结束抓包线程——静态
	static  int					n				= 1;
	static 	String				netIP			= null;
	String 	pattern								= "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\..*$";//正则表达式
	
	ARPFace(){
		Face();
		Listeners();
	}
	
	public void Face(){
		setTitle("ARP协议获取局域网内部活动主机的物理地址");
		setSize(960,800);			
		setLocation(2*screenSize.width/4,screenSize.height/8);     		
		add(topBar,BorderLayout.NORTH);
		topBar.add(lab);
		topBar.add(text);
		topBar.add(start);
        topBar.add(stop);
        topBar.add(clear);
        topBar.add(close);
        lab.setFont(buttonFont);
        text.setFont(buttonFont);
        start.setFont(buttonFont);
        stop.setFont(buttonFont);
        clear.setFont(buttonFont);
        close.setFont(buttonFont);
        textArea.setFont(new Font("宋体", Font.PLAIN, 20));
        textArea.setMargin(new Insets(3,10,3,10));
        textArea.setLineWrap(true);
        textArea.setDragEnabled(true);
        JScrollPane panel = new JScrollPane(textArea,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        getContentPane().add(panel, BorderLayout.CENTER);
	}
	
	public void Listeners(){
		start.addActionListener(this::Start);
		stop.addActionListener(this::Stop);
		clear.addActionListener(this::Clear);
		close.addActionListener(this::Close);
	}
	
	public void Start(ActionEvent event){
		ARPFace.netIP = text.getText();
		if (ARPFace.netIP != null && ARPFace.netIP.matches(pattern)){
			flag = true;
			ARPThread arpt = new ARPThread();
			arpt.start();
		}else{
			textArea.append("请您输入你需要查看的网络。\n");
			JOptionPane.showOptionDialog(null, "请输入正确的网络号","提示",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null, null, null);
		}
	}
	
	public void Stop(ActionEvent event){
		flag = false;
		try{
			Thread.sleep(2000);
			textArea.append("|**************已停止**************|\n");
		}catch (InterruptedException e) {		e.printStackTrace();		}
	}
	
	public void Clear(ActionEvent event){
		flag 			= false;
		ARPFace.n 		= 1;
		text.setText(null);
		ARPFace.netIP 	= null;
		try{
			Thread.sleep(2000);
			textArea.setText("");
		}catch (InterruptedException e) {		e.printStackTrace();		}
	}
	
	public void Close(ActionEvent event){
		System.exit(0);
	}
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		ARPFace arf = new ARPFace();
		arf.setVisible(true);
	}

}